<?php

namespace wechat\src;

/**
 * Description of Miniprogram
 *
 * @author admin
 */
use wechat\Wechat;
use wechat\lib\WXBizDataCrypt;

class Miniprogram extends SrcAbstract {

    /**
     * 登录凭证校验
     * @param string $jscode 登录时获取的 code;
     */
    public function code2Session($jscode) {
        if (!empty($this->openid) && !empty($this->session_key)) {
            return true;
        }
        $url = "https://api.weixin.qq.com/sns/jscode2session";
        $params = [
            'appid' => $this->wechat->appid,
            'secret' => $this->wechat->appsecret,
            'js_code' => $jscode,
            'grant_type' => 'authorization_code',
        ];
        $result = HttpRequest($url, $params, false);
        if ($result === false) {
            $this->setError(102, "取得凭证错误");
            return false;
        } else {
            $resultArray = json_decode($result, true);
            if (empty($resultArray['errcode']) || $resultArray['errcode'] == 0) {
                $this->openid = $resultArray['openid'];
                $this->session_key = $resultArray['session_key'];
                return $resultArray;
            } else {
                $this->setError(102, $resultArray['errcode'] . ":" . $resultArray['errmsg']);
                return false;
            }
        }
    }

    /**
     * 通过微信加密数据，取得用户信息
     * @param string $jscode 登录时获取的 code;
     * @param string $encryptedData 包括敏感数据在内的完整用户信息的加密数据
     * @param string $iv 加密算法的初始向量
     * @return mixed 返回解密后的用户信息
     */
    public function getUserinfo($jscode, $encryptedData, $iv) {
        $result = $this->code2session($jscode);
        if ($result !== false) {
            $pc = new WXBizDataCrypt($this->wechat->appid, $this->session_key);
            $errCode = $pc->decryptData($encryptedData, $iv, $userinfo);
            if ($errCode == 0) {
                $this->userinfo = json_decode($userinfo, true);
                return $this->userinfo;
            } else {
                $this->setError(103, "数据解析错误：{$errCode}");
                return false;
            }
        }
        return false;
    }

    /**
     * 获取小程序码,适用于需要的码数量极多的业务场景
     * @param string $page
     * @param string $scene
     * @return Buffer 图片Buffer信息
     */
    public function getUnlimited($page, $scene = "") {
        $wxCommon = $this->wechat->common();
        $accessToken = $wxCommon->AccessToken();
        if ($accessToken === false) {
            $this->setError(103, "微信接口调用凭证获取失败");
            return false;
        }
        $url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token={$accessToken}";
        $params = [
            'scene' => $scene,
            'page' => $page,
            'width' => 430,
            'is_hyaline' => true,
        ];
        $result = HttpRequest($url, json_encode($params), true);
        if (is_null(json_decode($result))) {
            return $result;
        } else {
            $resultArray = json_decode($result, true);
            $this->setError(102, "微信小程序码生成失败：{$resultArray['errcode']}：{$resultArray['errmsg']}");
            return false;
        }
    }

}
